<?php

namespace App\Http\Controllers\Crontab;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\yb\CouponModel;
use App\Models\yb\GoodsOrderDetailModel;
use App\Models\yb\GoodsOrderModel;
use App\Models\yb\NetworkStatisticsModel;
use App\Models\yb\UserCouponModel;

class CrontabController extends Controller
{

    /**
     * Get a JWT via given credentials.
     *  优惠券超时检测
     * @return \Illuminate\Http\JsonResponse
     */
    public function CouponTimeout()
    {

        UserCouponModel::where(['status'=>1])->where('validity_end','<',date('Y-m-d'))->update([
            'status'=>0,
        ]);

        return 'success';
    }

    /**
     * Get a JWT via given credentials.
     *  订单完成
     * @return \Illuminate\Http\JsonResponse
     */
    public function OrderTimeout()
    {
        $goods_data = GoodsOrderModel::where(['status'=>3])->where('service_date','>',date("Y-m-d", strtotime("+1 days")))->get();
        foreach($goods_data as $key=>$val){
            AdminUserModel::where(['id'=>$val['network_id']])->increment('sales_volume',$val['amount']);
        }

        GoodsOrderModel::where(['status'=>3])->where('service_date','>',date("Y-m-d", strtotime("+1 days")))->update([
            'status'=>4
        ]);

        return 'success';

    }

    /**
     * Get a JWT via given credentials.
     *  订单完成
     * @return \Illuminate\Http\JsonResponse
     */
    public function NetworkTimeout()
    {
        $statistics_data = array();

        $admin_user = AdminUserModel::where(['type'=>2,'status'=>1])->get();

        foreach($admin_user as $key=>$val){
            $statistics_data[$key]['network_id'] = $val['id'];
            $statistics_data[$key]['network_name'] = $val['name'];
            $statistics_data[$key]['start_time'] = date('Y-m-01', strtotime('-1 month'));
            $statistics_data[$key]['end_time'] = date('Y-m-t', strtotime('-1 month'));
            $statistics_data[$key]['order_number'] = GoodsOrderModel::where(['network_id'=>$val['id'],'status'=>1])->where('created_at','like','%'.date('Y-m', strtotime('-1 month')).'%')->count();
            $statistics_data[$key]['turnover_amount'] = GoodsOrderModel::where(['network_id'=>$val['id'],'status'=>1])->where('created_at','like','%'.date('Y-m', strtotime('-1 month')).'%')->sum('amount');
            $statistics_data[$key]['created_at'] = date('Y-m-d H:i:s');
        }

        NetworkStatisticsModel::insert($statistics_data);

        return 'success';

    }

     /**
     * Get a JWT via given credentials.
     *  订单取消
     * @return \Illuminate\Http\JsonResponse
     */
    public function OrderCancel()
    {

        $payment_time = date ( "Y-m-d H:i:s" , strtotime ( "-30 minute" ));

        $goods_order_data = GoodsOrderModel::where(['status'=>0])->where('payment_time','!=','')->where('payment_time','<',$payment_time)->get();

        foreach ($goods_order_data as $key=>$val){
            $goods_detail_data = GoodsOrderDetailModel::where('coupon_id','>',0)->where(['order_sn'=>$val['order_sn']])->get();
            foreach ($goods_detail_data as $k=>$v){
                UserCouponModel::where(['user_id'=>$v['user_id'],'coupon_id'=>$v['coupon_id']])->update(['is_used'=>0,'usage_time'=>'']);
            }
        }

        GoodsOrderModel::where(['status'=>0])->where('payment_time','!=','')->where('payment_time','<',$payment_time)->update(['status'=>5]);

        return 'success';

    }

}
